# -*- coding: utf-8 -*-

'''
/**
 * @addtogroup esm_get_table_info
 * esm_get_table_info
 * @{
 */
/**
 * @file        esm_get_table_info.py
 * @brief       XXXX
 * @note        XXXX
 * @author      靳普诏(puzhao.jin@hopechart.com)
 * @date        2023/08/26
 * @version     1.0

 * @par         修改日志
 * <table>
 * <tr><th>Date         <th>Version     <th>Author      <th> Description
 * <tr><td>2023/08/26   <td>1.0         <td>靳普诏       <td> 创建初始版本
 * @copyright   xxxx
 */
'''

import os
import sys

from pyazlib.EasyXlsx import TXlsx
from pyazlib.EasyXlsx import TXlsxScanDire

g_title = {"状态ID": 1, "状态函数名": 2, "状态说明": 3, "条件ID": 4, "条件函数名": 5, "条件说明": 6, "跳转后状态函数名": 7}
g_default_config = {"状态函数名前缀": "DefaultStateFunc", "条件函数名前缀": "DefaultCondFunc"}


'''
检查标题是否符合要求
param xlsx
return 
'''
def AssertXlsxTitle(xlsx: TXlsx):
    title = xlsx.sheetOneDimeScan("<state_machine>", cell_from='', row_pos=1, col_pos=1, direction=TXlsxScanDire.kToRight, end_flag=None, tag_count=len(g_title))

    i = 1
    for key in title:
        if g_title[key] == i:
            i += 1
        else:
            raise Exception("xlsx title error: %s" % key)

    if i < len(g_title) + 1:
        raise Exception("xlsx title error: len(g_title)")

    return True

def AssertXlsxConfig(xlsx: TXlsx):
    # config = xlsx.sheetOneDimeScan("<config>", cell_from='A2', direction=TXlsxScanDire.kToDown, end_flag=None, tag_count=len(g_default_config))
    
    # i = 1
    # config_list = g_default_config.keys()
    # for key in config:
    #     if config_list[key] == i:
    #         i += 1
    #     else:
    #         raise Exception("xlsx config error: %s" % key)

    # if i < len(g_default_config) + 1:
    #     raise Exception("xlsx config error: len(g_default_config)")

    return True



'''
条件函数信息
'''
class TEsmCondFuncInfo:
    def __init__(self, condition_function_name: str, condition_brief: str):
        self.function_name = condition_function_name
        self.brief = condition_brief



'''
状态函数信息
'''
class TEsmStateFuncInfo:
    def __init__(self, state_id: int, state_function_name: str, state_brief: str):
        self.id = state_id
        self.function_name = state_function_name
        self.brief = state_brief
        self.cond_list = []

    def AddCondition(self, condition_id: int, condition_function_name: str, condition_function_brief: str, jump_to_state_function_name: str):
        cond = [condition_id, condition_function_name, condition_function_brief, jump_to_state_function_name]
        self.cond_list.append(cond)
        return self.cond_list

    def Sort(self):
        self.cond_list.sort(key=lambda x: x[0])
        return 




class TEsmTableInfo:
    def __init__(self):
        # self.name = name
        # self.desc = desc
        # self.esm_state_list = esm_state_list
        self.esm_state_list = []
        self.config_info_dict = {}

    '''
    加载xlsx文件获取状态列表信息
    param self
    param xlsx_file_path
    return 
    '''
    def LoadInfoFromXlsx(self, xlsx_file_path):
        self.esm_state_list.clear()
        xlsx = TXlsx(xlsx_file_path)
        AssertXlsxTitle(xlsx)
        AssertXlsxConfig(xlsx)

        # 加载config信息
        self.__GetEsmConfigInfo(xlsx)

        # 加载状态转换信息
        row=2
        col=1
        have_next = True

        while have_next:
            (have_next, row, col) = self.__GetOneEsmInfo(xlsx, row, col)

        return

    def __GetEsmConfigInfo(self, xlsx: TXlsx):
        row = 2
        key_col = 1
        val_col = 2

        self.config_info_dict.clear()

        while True:
            key = xlsx.sheetCellValue("<config>", row, key_col)
            val = xlsx.sheetCellValue("<config>", row, val_col)
            if key in ["", "None", None]:
                break
            if val in ["", "None", None]:
                val = g_default_config[key]
            self.config_info_dict[key] = val
            row += 1
        return
        

    def __GetOneEsmInfo(self, xlsx: TXlsx, from_row=0, from_col=0):
        have_next = False
        row = from_row
        col = from_col

        # 获取状态列表信息
        state_id = xlsx.sheetCellValue("<state_machine>", row, col)
        state_func_name = xlsx.sheetCellValue("<state_machine>", row, col + 1)
        state_func_brief = xlsx.sheetCellValue("<state_machine>", row, col + 2)
        
        esm_state = TEsmStateFuncInfo(state_id, state_func_name, state_func_brief) 
        
        row = row + 1
        col = col + 3
        while True:
            cond_id = xlsx.sheetCellValue("<state_machine>", row, col)
            cond_func_name = xlsx.sheetCellValue("<state_machine>", row, col + 1)
            cond_func_brief = xlsx.sheetCellValue("<state_machine>", row, col + 2)
            cond_jump_state_name = xlsx.sheetCellValue("<state_machine>", row, col + 3)
            if cond_id in ["", "None", None]:
                break
            if cond_id in ["", "None", None] or cond_func_name in ["", "None", None] or cond_jump_state_name in ["", "None", None]:
                raise Exception("跳转条件填写有误")
            esm_state.AddCondition(cond_id, cond_func_name, cond_func_brief, cond_jump_state_name)
            row = row + 1
        
        self.esm_state_list.append(esm_state)
        
        next_row = row
        next_col = from_col
        if xlsx.sheetCellValue("<state_machine>", next_row, next_col) not in ["", "None", None]:
            have_next = True

        return (have_next, next_row, next_col)




if __name__ == "__main__":
    ## 获取当前脚本所在的目录
    SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
    # 输出目录路径
    print("SCRIPT_DIR: "+ SCRIPT_DIR)

    ## 获取脚本传参
    argv = sys.argv
    argc = len(argv)
    # 打印命令行参数
    #for i in range(1, argc):
        #print("参数", i, ":", argv[i])
        
    print("============= BIGEN: esm_get_table_info.py =============")




    print("============= END: esm_get_table_info.py =============")



## Generated on "2023-08-26 13:16:07" by the tool "gen_hq_file.py >> V20230509_1" 
